require "prefabutil" -- 服务 MakePlacer 函数.

local assets                       = {
    Asset("ANIM", "anim/sw_glass_tree_sapling.zip"),
    Asset("ANIM", "anim/sw_sapling.zip"),
}

----------------------------------------------------------------------------------------------------------------------

local function Sapling_DigUp(inst)
    local loot = nil

    if inst.components.lootdropper ~= nil then
        loot = inst.components.lootdropper:SpawnLootPrefab("sw_glass_tree_sapling_item")
    end

    inst:Remove()

    return loot -- Mods.
end

local function Sapling_OnDeploy(inst, pt)
    local sapling = SpawnPrefab("sw_glass_tree_sapling")
    sapling.SoundEmitter:PlaySound("dontstarve/wilson/plant_tree")
    sapling.Transform:SetPosition(pt:Get())

    if inst.components.stackable ~= nil then
        inst.components.stackable:Get():Remove()
    else
        inst:Remove()
    end

    return sapling -- Mods.
end

----------------------------------------------------------------------------------------------------------------------

local function SaplingFn()
    local inst = CreateEntity()

    inst.entity:AddTransform()
    inst.entity:AddAnimState()
    inst.entity:AddSoundEmitter()
    inst.entity:AddMiniMapEntity()
    inst.entity:AddNetwork()

    inst:SetDeploySmartRadius(DEPLOYSPACING_RADIUS[DEPLOYSPACING.PLACER_DEFAULT] / 2) -- seed deployspacing/2

    inst.AnimState:SetBank("sw_glass_tree_sapling")
    inst.AnimState:SetBuild("sw_glass_tree_sapling")
    inst.AnimState:PlayAnimation("idle")

    inst:AddTag("event_trigger")

    inst.entity:SetPristine()

    if not TheWorld.ismastersim then
        return inst
    end

    inst:AddComponent("lootdropper")

    inst:AddComponent("inspectable")

    inst:AddComponent("workable")
    inst.components.workable:SetWorkAction(ACTIONS.DIG)
    inst.components.workable:SetOnFinishCallback(Sapling_DigUp)
    inst.components.workable:SetWorkLeft(1)

    MakeHauntableWork(inst)

    return inst
end

local function SaplingItemFn()
    local inst = CreateEntity()

    inst.entity:AddTransform()
    inst.entity:AddAnimState()
    inst.entity:AddSoundEmitter()
    inst.entity:AddNetwork()

    MakeInventoryPhysics(inst)

    inst.AnimState:SetBank("sw_sapling")
    inst.AnimState:SetBuild("sw_sapling")
    inst.AnimState:PlayAnimation("glass")

    inst:AddTag("deployedplant")

    MakeInventoryFloatable(inst, "large", 0.2, 0.55)

    inst.entity:SetPristine()

    if not TheWorld.ismastersim then
        return inst
    end

    inst:AddComponent("inventoryitem")

    inst:AddComponent("inspectable")

    inst:AddComponent("tradable")

    inst:AddComponent("deployable")
    inst.components.deployable:SetDeploySpacing(DEPLOYSPACING.PLACER_DEFAULT)
    inst.components.deployable.ondeploy = Sapling_OnDeploy

    MakeHauntableLaunch(inst)

    return inst
end

return
    Prefab("sw_glass_tree_sapling", SaplingFn, assets),
    Prefab("sw_glass_tree_sapling_item", SaplingItemFn, assets),
    MakePlacer("sw_glass_tree_sapling_item_placer", "sw_glass_tree_sapling", "sw_glass_tree_sapling", "idle")
